﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HW_f1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            LoadDrivers();
        }

        private void LoadDrivers()
        {
            TreeNode rootNode = new TreeNode("My Computer");
            rootNode.Tag = "MyComputer";
            treeView1.Nodes.Add(rootNode);

            DriveInfo[] drives = DriveInfo.GetDrives();
            foreach (DriveInfo drive in drives)
            {
                if (drive.IsReady)
                {
                    TreeNode driveNode = new TreeNode(drive.Name);
                    driveNode.Tag = drive.RootDirectory;
                    rootNode.Nodes.Add(driveNode);
                }
            }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode selectedNode = e.Node;

            if (selectedNode.Tag is DirectoryInfo)
            {
                DirectoryInfo directoryInfo = (DirectoryInfo)selectedNode.Tag;
                LoadSubDirectories(directoryInfo, selectedNode);
                LoadFiles(directoryInfo, selectedNode);
            }
        }

        private void LoadSubDirectories(DirectoryInfo directoryInfo, TreeNode parentNode)
        {
            parentNode.Nodes.Clear();

            try
            {
                DirectoryInfo[] subDirectories = directoryInfo.GetDirectories();
                foreach (DirectoryInfo subDirectory in subDirectories)
                {
                    TreeNode node = new TreeNode(subDirectory.Name);
                    node.Tag = subDirectory;
                    parentNode.Nodes.Add(node);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error loading subdirectories: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void LoadFiles(DirectoryInfo directoryInfo, TreeNode parentNode)
        {
            listView1.Items.Clear();
            ColumnHeader ch = new ColumnHeader();
            ch.Text = "文件名";
            ch.Width = 100;
            ch.TextAlign = HorizontalAlignment.Left;
            this.listView1.Columns.Add(ch);


            try
            {
                FileInfo[] files = directoryInfo.GetFiles();
                foreach (FileInfo file in files)
                {
                    ListViewItem item = new ListViewItem(file.Name);
                    item.SubItems.Add(file.LastWriteTime.ToString());
                    item.Tag = file;
                    listView1.Items.Add(item);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("Error loading files: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                ListViewItem selectedItem = listView1.SelectedItems[0];
                if (selectedItem.Tag is FileInfo)
                {
                    FileInfo fileInfo = (FileInfo)selectedItem.Tag;
                    if (fileInfo.Extension == ".txt")
                    {
                        try
                        {
                            string text = File.ReadAllText(fileInfo.FullName);
                            MessageBox.Show(text, "Text File Contents", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("Error opening file: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                }
            }
        }
    }
}
